<?php
namespace Admin\Controller;
/**
 * 客户日志
 */
class CustomerLogController extends AdminController{
	
	/**
	 * 日志列表
	 */
	public function index() {
		$map = array();

		if ( $this->_userInfo['group_id'] == '5') {
			$map['uid'] = UID;
			
		}
		$key = I('get.key');
		if ($key) {
			$where['name'] = array('like',"%$key%"); //姓名
			$where['phone'] = array('like',"%$key%"); //电话
			$where['phone1'] = array('like',"%$key%"); //电话
			$where['_logic'] = 'OR';
			$map['_complex'] = $where;
		}
		
		$list = $this->lists('CustomerLog', $map, 'id DESC');
		
		$this->assign('_list',$list);
		
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);

		$this->meta_title = '客户日志列表';
		$this->display();
		
		$this->display();
	}
	
	/**
	 * 浏览与添加日志
	 */
	public function view($cid = NULL) {
	
		if (IS_POST) {
			$_POST['cid'] = $cid;
			$_POST['uid'] = UID;
			$_POST['name'] = M('Member')->where(array('uid' => UID))->getField('nickname');
			$_POST['company'] = M('Customer')->where(array('id' => $cid))->getField('company');
			$CustomerLog = D('CustomerLog');
			$res = $CustomerLog->update();
			if(!$res){
				$this->error($CustomerLog->getError());
			}else{
				$this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
			}
				
		} else {
			if (empty($cid)) $this->error('参数错误');
			$info = D('Customer')->info($cid);
			if (!IS_ROOT) {
				if ($info['uid'] != UID) {
					$this->error('参数错误');
				}
			}
			$this->assign('info', $info);
			$this->assign('cid', $cid);
			$this->meta_title = '客户日志';
			//读取客户记录
			
			$map['cid'] = $cid;
			$list = $this->lists('CustomerLog', $map, 'id DESC');

			$this->assign('_list',$list);
			
			// 记录当前列表页的cookie
			Cookie('__forward__',$_SERVER['REQUEST_URI']);
			$this->display();
		}
	
	}
	
	/**
	 * 删除
	 */
	public function del($id) {
		if (empty($id)) $this->error('参数错误');
		$CustomerLog = M('CustomerLog');
		$res = $CustomerLog->delete($id);
		if(!$res){
			$this->error($CustomerLog->getError());
		}else{
			$this->success('删除成功', Cookie('__forward__'));
		}
	}

	
}